package com.sctvcloud.qingchuan.ui.adapter.holder;

import android.content.Context;
import android.util.Pair;
import android.view.View;
import android.widget.TextView;

import com.gridsum.tracker.GridsumWebDissector;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.qingchuan.R;
import com.sctvcloud.qingchuan.beans.HomeListItem;
import com.sctvcloud.qingchuan.beans.NewsItem;
import com.sctvcloud.qingchuan.ui.activities.QuickActivity;
import com.sctvcloud.qingchuan.ui.adapter.MarqueeAdapter;
import com.sctvcloud.qingchuan.ui.utils.SkipUtil;
import com.sctvcloud.qingchuan.ui.widget.marqueeview.MarqueeView;
import com.sctvcloud.qingchuan.ui.widget.marqueeview.MultiItemTypeAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 重要公告
 */
public class HomeNoticeHolder extends BaseAbsHolder<HomeListItem> implements View.OnClickListener {

    @BindView(R.id.marqueeView)
    MarqueeView marqueeView;
    @BindView(R.id.to_icon)
    CustomFontTextView moreTextView;

    private List<NewsItem> data;
    private String channelUrl;
    private MarqueeAdapter mAdapter;

    public HomeNoticeHolder(final Context context, View itemView) {
        super(context, itemView);
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(HomeListItem item) {
        data = item.getNewsItemList();
        channelUrl = item.getSubUrl();
        if (ListUtils.isListValued(data)) {
            if (data.size() > 3) {
                data = data.subList(0, 3);
            }
            mAdapter = new MarqueeAdapter(context, data);
            marqueeView.setAdapter(mAdapter);
            marqueeView.startFlip();
            mAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(int position, View view) {
                    NewsItem item = mAdapter.getItem(position);
                    if (item != null) {
                        ArrayList<Pair<String, String>> pairs = new ArrayList<>();
                        pairs.add(new Pair<>("页面", "首页"));
                        pairs.add(new Pair<>("栏目", "精选"));
                        pairs.add(new Pair<>("位置", "24小时"));
                        ArrayList<Pair<String, String>> tempPair = new ArrayList<>(pairs);
                        tempPair.add(new Pair<>("行为类型", "点击"));

                        if (activity != null) {
                            GridsumWebDissector.getInstance().trackEvent(activity, "", item.getNewsTitle(), "", 200, tempPair);
                        } else if (fragment != null) {
                            GridsumWebDissector.getInstance().trackEvent(fragment, "", item.getNewsTitle(), "", 200, tempPair);
                        }
                        HashMap<String, String> map = new HashMap<>();
                        for (Pair<String, String> p : pairs) {
                            map.put(p.first, p.second);
                        }
                        SkipUtil.skipWithNewsItem(context, item, map);
                    }
                }
            });

            moreTextView.setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        QuickActivity.start(channelUrl);
    }
}

